<template>
  <div class="app-container">
    <!--    隐藏域 记录id-->
    <input type="hidden" v-model="record.id"/>
    <el-form ref="form" :model="record" label-width="120px">
      <el-form-item label="借阅卡编号">
        <el-input v-model="record.student.sname"/>
      </el-form-item>
      <el-form-item label="图书编号">
        <el-input v-model="record.book.name"/>
      </el-form-item>
      <el-form-item label="借书日期">
        <el-date-picker
          v-model="record.bdate"
          type="date"
          placeholder="请选择时间"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item label="还书日期">
        <el-date-picker
          v-model="record.rdate"
          type="date"
          placeholder="请选择时间"
          style="width: 100%"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">修改信息</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  data() {
    return {
      record: {
        id: 0,
        caid: '',
        bid: '',
        bdate: '',
        rdate: ''
      },
      card: {
        sname: ''
      },
      book: {
        name: ''
      },
    }
  },
  mounted() {
    this.record = this.$store.state.user.record;
  },
  methods: {
    onSubmit() {
      request({
        url: "/edu/student/selectIdByName.do?name=" + this.record.student.sname,
        method: "GET"
      }).then(res => {
        if (res.code == 0) {
          this.record.caid = res.data.id;

          request({
            url: "/library/book/selectIdByName.do?name=" + this.record.book.name,
            method: "GET"
          }).then(res => {
            if (res.code == 0) {
              this.record.bid = res.data.id;

              request({
                url: "/library/record/update.do",
                method: "POST",
                data: this.record
              }).then(res => {
                if (res.code == 0) {
                  //跳转到列表页面
                  this.$router.push("/recordlist");
                }
              });
            }
          })
        }
      })
    },

    // 请求接口 实现用户新增接口的调用

    onCancel() {
      this.$message({
        message: 'cancel!',
        type: 'warning'
      });
      this.$router.push("/recordlist");
    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}
</style>

